import * as actionTypes from './actionTypes'
import axios from 'axios'
import * as api from '../../public/api'
import Immutable from 'immutable'
import * as message from '../../components/Msg/msg'
import qs from 'qs'
import { setUInfo } from '../../session/session'

// 获取登录信息
export const userInfo = (param) => ({ type: actionTypes.userInfo, data: param })

export const setUserInfo = (param) => (dispatch) => {
  const { userName, password } = param
  return new Promise((resolve, reject) => {
    axios.get(api.userInfo + '?username=' + userName + '&password=' + password).then((response) => {
      const { code, data, msg } = response.data
      const { pass, readerInfo } = data
      if (code === 0) {
        if (pass) {
          readerInfo.password = password
          setUInfo(readerInfo)
          dispatch(userInfo(Immutable.fromJS(readerInfo)))
          resolve()
        } else {
          reject()
        }
      } else {
        reject(msg)
      }
    }).catch(e => {
      message.error(e.message)
      reject()
    })
  })
}

